from django.shortcuts import render
from django.http import JsonResponse,HttpResponse
from .models import NumberTable,ComputerStatus,IpAssets,OAssets
from networkstatus.models import UrlKeyword
from django.db import connection
from django.views.decorators.csrf import csrf_exempt
from django.forms.models import model_to_dict

# Create your views here.
# 资产新增模块
def saveNumber(request):
    if request.method == "POST":
        number = request.POST.get("number"," ")
        name = request.POST.get("name"," ")
        category = request.POST.get("category"," ")
        num = request.POST.get("num"," ")
        table = request.POST.get("table"," ")
        seat_number = request.POST.get("seat_number"," ")
        asset_users = request.POST.get("asset_users"," ")
        remarks = request.POST.get("remarks"," ")
        cursor = connection.cursor()


        in_tab = request.POST.get("in_tab","")
        if in_tab == "默认最后时间":
            in_tab = NumberTable.objects.all().last().tab_name

        sql = "insert into `{}` (it_number,asset_users,asset_name,seat_number,category,it_table) value('{}','{}','{}','{}','{}','{}')".format(in_tab,number,asset_users,name,seat_number,category,table)
        print(sql)
        cursor.execute(sql)

        if table == "computerstatus_ipassets":
            t = IpAssets()
            t.ip_number = number
            t.ip_name = name
            t.ip_category = category
            t.ip_num = num
            t.ip_seat_number = seat_number
            t.ip_asset_users = asset_users
            t.ip_remarks = remarks
            t.save()
        else:
            t = OAssets()
            t.o_number = number
            t.o_name = name
            t.o_category = category
            t.o_num = num
            t.o_seat_number = seat_number
            t.o_asset_users = asset_users
            t.o_remarks = remarks
            t.save()

        return JsonResponse({"status":"200","msg":"成功，数据添加至："+in_tab})

# 系统管理 - 自动更新数据管理
@csrf_exempt
def autoZichan(request):
    shu_num = 2
    ye_num = 8
    titleClass = "用户管理 - 每日登录接口管理"

    if request.method == "GET":
        status = ComputerStatus.objects.all()
        # print(status)
        return render(request, "autozichan.html", locals())

    if request.method == "POST":
        id = request.POST.get('id', "")
        it_number = request.POST.get("it_number","")
        try:
            status = ComputerStatus.objects.filter(id=id).update(it_number=it_number)
        except Exception as e:
            print(e)
            return JsonResponse({"status":"404","msg":"更新失败"})

    status = ComputerStatus.objects.all()
    return render(request, "autozichan.html", locals())


# 系统管理 - 自动更新数据管理 返回查询信息接口
@csrf_exempt
def getauto(request):

    if request.method == 'POST':
        id = request.POST.get('id',"")
        status = ComputerStatus.objects.get(id=id)

    if status:
        di = model_to_dict(status,exclude=['c_time'])
        return JsonResponse({"number":di,"code":"200"})

    return JsonResponse({"number":"查询错误","code":"404"})


# 自动更新MAC修改 显示接口
def Ipassets(request):
    shu_num = 2
    ye_num = 7
    titleClass = "用户管理 - 自动更新MAC修改"
    listtab = ['资产编号', '资产名称', '具体位置', '资产使用者', 'MAC地址', '所属公司','操作']
    list1 = IpAssets.objects.values_list('ip_number','ip_name','ip_seat_number','ip_asset_users','ip_mac','ip_remarks')
    return render(request,"ipassets.html",locals())


# 自动更新MAC修改 接口
@csrf_exempt
def editipassets(request):
    number = request.POST.get('number',"")
    name = request.POST.get('name',"")
    seat_number = request.POST.get('seat_number',"")
    asset_users = request.POST.get('asset_users',"")
    remarks = request.POST.get('remarks',"")
    mac = request.POST.get('mac',"")
    mac = mac.strip()
    mac = mac.replace('：',":")

    try:
        t = IpAssets.objects.get(ip_number=number)
        t.ip_name = name
        t.ip_seat_number = seat_number
        t.ip_asset_users = asset_users
        t.ip_remarks = remarks
        t.ip_mac = mac
        t.save()
    except Exception as e:
        print(e)
        return JsonResponse({'code':'400','msg':'错误'})

    return JsonResponse({'code':'200','msg':'更新成功'})


# 防火墙URL过滤规则
@csrf_exempt
def updataURL(request):
    shu_num = 2
    ye_num = 6
    titleClass = "用户管理 - 防火墙URL过滤规则"
    listtab = ['序列', 'URL', '对应网址', '操作']
    list1 = UrlKeyword.objects.values_list()
    # GET 请求时，只是查询，不做任何动作。
    if request.method == "GET":
        return render(request,"urlfilter.html",locals())

    staid = request.POST.get("staid","")
    if staid == "1":
        add_url = request.POST.get("add_url","")
        add_keyword = request.POST.get("add_keyword","")
        t = UrlKeyword()
        t.url = add_url
        t.Keyword = add_keyword
        t.save()
    # staid 为2时，代表修改的接口
    else:
        number = request.POST.get("number","")
        edit_url = request.POST.get("edit_url","")
        edit_keyword = request.POST.get("edit_keyword","")
        t = UrlKeyword.objects.get(id=number)
        t.url = edit_url
        t.Keyword = edit_keyword
        t.save()

    return render(request,"urlfilter.html",locals())


